<?php
/**
 * Created by PhpStorm.
 * User: Machenike
 * Date: 2019/9/11
 * Time: 12:45
 */

namespace App\Project;


class ShengJinApi
{
    public $notice = "注:本利所涉及现金单位都是‘分’而非元";

    public $vi = "af47d8sgsdf6sdf8g15sa635f498g15h486d7s4g5";
    
    public $project = 2;

    public $baseUrl = "http://shengjin.com";


    /**
     * 获取所有配置
     * @return string
     */
    public function getConfig()
    {
        //实时获取，或者设置缓存增加读写速度
        //todo 如果使用本地缓存请自行编写
        $api = "/api/get_money_config";
        $re = $this->_api($api,[]);
        $config = json_decode($re,false);
        $data = [];
        foreach ($config->d as $confRow) {
            $data[$confRow->k] = $confRow->v;

        }
        if ($data) {
            return $this->_createResult(1,"获取成功",$data);
        }else{
            return $this->_createResult(0,"参数错误");
        }
    }

    /**
     * @param $key
     * @return bool
     */
    public function getConfigByKey($key)
    {
        //获取积分充值的兑换比例
        $confStr = $this->getConfig();
        $conf = json_decode($confStr,true);
        if ($conf["s"] == 0) {
            return false;
        }else{
           return $conf["d"][$key];
        }

    }

    /**
     * 用户现金充值积分
     * @param $user
     * @param $money int 现金金额，单位为分
     * @return array|mixed
     */
    public function addJf4Charge($user,$money)
    {
        $api = "/api/money_add";
        $times = $this->getConfigByKey("NUM_JF_CHARGE");
        $money = floor($money * $times);
        $data["uid"] = $user;
        $data["money"] = $money;
        $data["moneyType"] = 3;//赠送积分
        $data["code"] = 104;//现金充值
        $data["tips"] = "积分充值";
        return $this->_api($api,$data);
    }

    /**
     * 购物积分返现
     * @param $user
     * @param $money int 购物金额
     */
    public function addJf4Buy($user,$money)
    {
        $configStr = $this->getConfig();
        $config = json_decode($configStr,true)["d"];
        $money_qq = floor($money * $config["PERCENT_BUY_QQ"]/100);
        $money_ly = floor($money * $config["PERCENT_BUY_LY"]/100);
        $money_gy = floor($money * $config["PERCENT_BUY_GY"]/100);
        $money_zsjf = floor($money * $config["PERCENT_BUY_ZSJF"]/100);

        $api = "/api/money_add";
        $data["uid"] = $user;
        $data["money"] = $money_zsjf;
        $data["moneyType"] = 3;//赠送积分
        $data["code"] = 111;//购物返利
        $data["tips"] = "购物返利";
        $this->_api($api,$data);

        $data["uid"] = $user;
        $data["money"] = $money_qq;
        $data["moneyType"] = 4;//期权
        $data["code"] = 111;//购物返利
        $data["tips"] = "购物返利";
        $this->_api($api,$data);

        $data["uid"] = $user;
        $data["money"] = $money_ly;
        $data["moneyType"] = 5;//旅游
        $data["code"] = 111;//购物返利
        $data["tips"] = "购物返利";
        $this->_api($api,$data);

        $data["uid"] = $user;
        $data["money"] = $money_gy;
        $data["moneyType"] = 6;//公益
        $data["code"] = 111;//购物返利
        $data["tips"] = "购物返利";
        $this->_api($api,$data);

    }

    /**
     * 积分赠送好友
     * @param $fromUser
     * @param $toUser
     * @param $money
     * @return string
     */
    public function addJfFromFriend($fromUser,$toUser,$money)
    {

        $api = "/api/money_add";
        $data["uid"] = $fromUser;
        $data["money"] = abs($money) * -1;
        $data["moneyType"] = 2;//消费积分
        $data["code"] = 203;//赠送好友
        $data["tips"] = "赠送好友";
        $re = $this->_api($api,$data);
        if ($re) {
            $json = json_decode($re);
            if ($json->s == 1) {
                $x = $this->getConfigByKey("PERCENT_GIVE_TO_FRIEND");
                if (!is_numeric($x)) {$x = 1;}
                $data["uid"] = $toUser;
                $data["money"] = floor($money * $x);
                $data["moneyType"] = 3;//赠送积分
                $data["code"] = 103;//好友赠送
                $data["tips"] = "好友赠送";
                $re=$this->_api($api,$data);
                $json = json_decode($re);
                return $this->_createResult(1,$json->i,$json->d);
            }else{
                return $this->_createResult(0,$json->i,$json->d);
            }
        }else{
            return $this->_createResult(500,"系统发生错误");
        }

    }

    /**
     * 现金购买产品后，增加团队业绩
     * @param $user int 用户推荐人
     * @param $money
     * @param $moneyType
     */
    public function addYj2Parent($user,$money,$moneyType = 1)
    {

        $api = "/api/money_add_static";
        $data["uid"] = $user;
        $data["money"] = $money;
        $data["moneyType"] = $moneyType;//赠送积分
        $data["code"] = 301;//购物返利
        $data["tips"] = "直推团队业绩";
        $this->_api($api,$data);
    }
    
    private function _api($url,$data = []){
        $url = $this->baseUrl.$url;
        $time = time();
        $token = $this->_createToken($this->vi,$time,$this->project);
        $head = array("vi:$this->vi", "t:$time", "token:$token", "project:$this->project",);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $head);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }
    private function _createToken($vi,$time,$project)
    {
        if ($time < time() - 300 || $time > time() + 300) {
            die("time out");
        }
        return sha1($vi . $time . $project . $time . $vi );
    }
    private function _createResult($s,$i,$d=[]){
        return json_encode(array("s"=>$s,"i"=>$i,"d"=>$d,));
    }
}